package com.one.servlet;

import com.one.pojo.Cart;
import com.one.service.cartService;
import com.one.service.impl.cartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author LJ
 * @create 2022-04-25-16:45
 */
@WebServlet("/saveCart")
public class saveCartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String number = req.getParameter("number");
       ;
        String datetime = req.getParameter("datetime");
        Long aLong = Long.valueOf(datetime);
        Date date=new Date(aLong);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        String bookName = req.getParameter("bookName");
        String price = req.getParameter("price");
        BigDecimal bigDecimal = new BigDecimal(price);
        System.out.println("价格"+bigDecimal);
        String start = req.getParameter("start");
        String userid = req.getParameter("userid");
        Cart cart=new Cart();
        cart.setUserId(Integer.parseInt(userid));
        cart.setStart(Integer.parseInt(start));
        cart.setData(format);
        cart.setName(bookName);
        cart.setNumber(Integer.parseInt(number));
        cart.setPrice(bigDecimal);
        cartService service=new cartServiceImpl();
        boolean save = service.save(cart);
        if (save){
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
    }
}
